Public Class MyOutputs
    Private Const CASE_FILE As String = "ALL.dat"
    Private Const FAC_FILE As String = "FAC.dat"
    Private Const PREQ_FILE As String = "PREQ.exe"
    Private Const PRNES_FILE As String = "PRNES.exe"
    Private Const PRNET_FILE As String = "PRNET.exe"
    Private Const TEMP_DIR As String = "Temp"

    Private WithEvents m_objMySections As MySections
    Private m_blnBuilding As Boolean
    Private m_blnCanceled As Boolean
    Private m_blnUpdated As Boolean
    Private m_intErrorCode As Integer
    Private m_intOutputDirIndex As Integer = -1
    Private m_objOutputFileList As New List(Of OutputFile)
    Private m_strOutputDirPath As String
    Private m_strPublishDirPath As String

    Public Event Status(ByVal msg As String)

    Public Enum OptionConstants
        Type
        Group
        Voltage
        Special
        Region
    End Enum

    Public Structure OutputData
        Public Xdata As Double()
        Public Ydata As Double()
    End Structure

    Public Structure OutputFile
        Public Title As String
        Public Name As String
        Public Extension As String
    End Structure

    Protected Overrides Sub Finalize()
        If m_strOutputDirPath Is Nothing Then
            'Delete temp output dir 
            With My.Computer.FileSystem
                Try
                    If .DirectoryExists(Me.OutputDirPath) Then .DeleteDirectory(Me.OutputDirPath, FileIO.DeleteDirectoryOption.DeleteAllContents)
                Catch ex As Exception
                End Try
            End With
        End If
    End Sub

    Public Sub New(ByVal sections As MySections)
        m_objMySections = sections
    End Sub

    Public Overloads Sub Build()
        m_blnBuilding = True

        If Not m_blnUpdated Then
            Me.BeginOutput()
            Me.DoOutput()
            Me.EndOutput()
        End If
        Me.ReadOutputDir()

        m_blnUpdated = Not m_blnCanceled

        m_blnBuilding = False
    End Sub

    Public Overloads Sub Build(ByVal fromDirPath As String)
        m_blnBuilding = True

        m_strOutputDirPath = fromDirPath

        Me.ReadOutputDir()

        m_blnUpdated = (Me.FileList.Count > 0)

        m_blnBuilding = False
    End Sub

    Public ReadOnly Property Building() As Boolean
        Get
            Return m_blnBuilding
        End Get
    End Property

    Public Sub Cancel()
        m_blnCanceled = True
    End Sub

    Public Function DataList(ByVal name As String) As List(Of OutputData)
        Dim intColumns As Integer
        Dim objDataList As New List(Of OutputData)
        Dim objDataValues As New List(Of Double())
        Dim objTextFieldParser As New FileIO.TextFieldParser(My.Computer.FileSystem.CombinePath(Me.OutputDirPath, name))
        Dim objValueList As List(Of Double)

        While objTextFieldParser.LineNumber <> -1
            Dim astrFields As String() = objTextFieldParser.ReadLine.Split(New String() {" ", ","}, StringSplitOptions.RemoveEmptyEntries)
            Dim dblValue As Double

            If intColumns = 0 Then
                If name.StartsWith("BG") Then
                    intColumns = astrFields.Length
                Else
                    intColumns = 2
                End If
            End If            
            objValueList = New List(Of Double)
            For intIndex As Integer = 0 To intColumns - 1
                Try
                    dblValue = Double.Parse(astrFields(intIndex).Replace(".", Globalization.NumberFormatInfo.CurrentInfo.NumberDecimalSeparator), Globalization.NumberStyles.Any)
                Catch
                    Continue While
                End Try

                objValueList.Add(dblValue)
            Next

            objDataValues.Add(objValueList.ToArray)
        End While

        For intIndex = 1 To intColumns - 1
            Dim objOutputData As New OutputData

            objValueList = New List(Of Double)
            For Each adblValues As Double() In objDataValues
                objValueList.Add(adblValues(0))
            Next
            objOutputData.Xdata = objValueList.ToArray

            objValueList = New List(Of Double)
            For Each adblValues As Double() In objDataValues
                objValueList.Add(adblValues(intIndex))
            Next
            objOutputData.Ydata = objValueList.ToArray

            objDataList.Add(objOutputData)
        Next

        Return objDataList
    End Function

    Public Function ErrorCode() As Integer
        Return m_intErrorCode
    End Function

    Public Overloads Function FileList() As List(Of OutputFile)
        Return m_objOutputFileList
    End Function

    Public Overloads Function FileList(ByVal indexType As Integer, Optional ByVal subIndexType As Integer = 0, Optional ByVal value As Double = 0, Optional ByVal specialIndexType As Integer = 0, Optional ByVal region As Integer = -1) As List(Of OutputFile)
        Dim objFileList As New List(Of OutputFile)

        For Each objOutputFile As OutputFile In Me.FileList
            Dim blnAdd As Boolean = False
            Dim strExtension As String = objOutputFile.Extension
            Dim strFullName As String = objOutputFile.Name
            Dim strName As String = objOutputFile.Name.Substring(0, strFullName.Length - (strExtension.Length + 1))

            If strName = "FF" Then Continue For

            Select Case indexType
                Case 0 'Thermodynamic Equilibrium
                    blnAdd = (strExtension = "000")

                Case 1 'Non-Equilibrium
                    Dim blnGapEnergy As Boolean = IsNumeric(strName.Substring(0, 2))
                    Dim blnMatchRegion As Boolean = blnGapEnergy AndAlso (Integer.Parse(strName.Substring(0, 2)) = region + 1)
                    Dim blnNonEqFile As Boolean

                    If strExtension = "000" Then
                        blnNonEqFile = False
                    Else
                        Dim strCode As String

                        Select Case subIndexType
                            Case 0 'No Bias Light present
                                If value > 0 Then
                                    If value < 10 Then
                                        strCode = "0" + (value * 100).ToString("00#")
                                    Else
                                        strCode = "A" + (value * 10).ToString("0#")
                                    End If
                                Else
                                    If value > -10 Then
                                        strCode = "2" + (Math.Abs(value) * 100).ToString("00#")
                                    Else
                                        strCode = "4" + (Math.Abs(value) * 10).ToString("0#")
                                    End If
                                End If
                                blnNonEqFile = (strExtension = strCode)

                            Case 1 'Bias Light present
                                If value > 0 Then
                                    If value < 10 Then
                                        strCode = "1" + (value * 100).ToString("00#")
                                    Else
                                        strCode = "B" + (value * 10).ToString("0#")
                                    End If
                                Else
                                    If value > -10 Then
                                        strCode = "3" + (Math.Abs(value) * 100).ToString("00#")
                                    Else
                                        strCode = "5" + (Math.Abs(value) * 10).ToString("0#")
                                    End If
                                End If
                                blnNonEqFile = (strExtension = strCode)

                            Case 2 'Short Circuit conditions
                                blnNonEqFile = (strExtension = "1000")

                            Case 3 'Maximun Power conditions
                                blnNonEqFile = (strExtension = "FFFF")

                            Case 4 'Open Circuit Voltage conditions
                                blnNonEqFile = (strExtension = "VOCV")

                            Case Else
                                Throw New ArgumentException
                        End Select
                    End If

                    Select Case specialIndexType
                        Case 0 'None
                            Select Case strName
                                Case "AUR", "RAR", "SRR", "DO"
                                Case Else
                                    blnAdd = blnNonEqFile AndAlso Not blnGapEnergy
                            End Select

                        Case 1 'Direct and Auger Recombination
                            Select Case strName
                                Case "AUR", "RAR", "SRR"
                                    blnAdd = blnNonEqFile
                            End Select

                        Case 2 'DPM - Stabilized State of a-Si devices
                            blnAdd = blnNonEqFile AndAlso (strName = "DO")

                        Case 3 'Gap Energy: Donor Tail
                            blnAdd = blnNonEqFile AndAlso blnMatchRegion AndAlso (strName.EndsWith("DT"))

                        Case 4 'Gap Energy: Acceptor Tail
                            blnAdd = blnNonEqFile AndAlso blnMatchRegion AndAlso (strName.EndsWith("AT"))

                        Case 5 'Gap Energy: Donor Midgap States
                            blnAdd = blnNonEqFile AndAlso blnMatchRegion AndAlso (strName.EndsWith("DM"))

                        Case 6 'Gap Energy: Acceptor Midgap States
                            blnAdd = blnNonEqFile AndAlso blnMatchRegion AndAlso (strName.EndsWith("AM"))

                        Case 7 'Gap Energy: Donor-like Gaussian States
                            blnAdd = blnNonEqFile AndAlso blnMatchRegion AndAlso (strName.EndsWith("DG"))

                        Case 8 'Gap Energy: Acceptor-like Gaussian States
                            blnAdd = blnNonEqFile AndAlso blnMatchRegion AndAlso (strName.EndsWith("AG"))

                        Case 9 'Gap Energy: Amphoteric-like Gaussian States
                            blnAdd = blnNonEqFile AndAlso blnMatchRegion AndAlso (strName.EndsWith("MG") OrElse strName.EndsWith("0G") OrElse strName.EndsWith("PG") OrElse strName.EndsWith("MG"))

                        Case 10 'Gap Energy: Conduction Band
                            blnAdd = blnNonEqFile AndAlso blnMatchRegion AndAlso (strName.EndsWith("CB"))

                        Case 11 'Gap Energy: Valence Band
                            blnAdd = blnNonEqFile AndAlso blnMatchRegion AndAlso (strName.EndsWith("VB"))

                        Case 12 'Gap Energy: Donor-like Discrete States
                            blnAdd = blnNonEqFile AndAlso blnMatchRegion AndAlso (strName.EndsWith("DD"))

                        Case 13 'Gap Energy: Acceptor-like Discrete States
                            blnAdd = blnNonEqFile AndAlso blnMatchRegion AndAlso (strName.EndsWith("DA"))

                        Case 14 'Gap Energy: General Files
                            If blnNonEqFile AndAlso blnMatchRegion AndAlso strName.Length > 3 Then
                                Select Case strName.Substring(strName.Length - 3)
                                    Case "DOS", "TNT", "TPT", "TND", "TPD", "TNA", "TPA", "TUA", "TRA", "TRD", "TRT", "TRH"
                                        blnAdd = True
                                End Select
                            End If

                        Case Else
                            Throw New ArgumentException
                    End Select

                Case 2 'Characteristic Output Device Curves
                    Select Case subIndexType
                        Case 0 'Regular Dark J-V and Light J-V
                            Select Case strFullName
                                Case "JD.F", "JD.R", "JD.DBT"
                                    blnAdd = True

                                Case "JL.DBT"
                                    blnAdd = (CInt(m_objMySections.Section1.ILIGHT1) = 1)
                            End Select

                        Case 1 'J-V corrected by Rs and Rsh
                            Select Case strFullName
                                Case "JR.F", "JR.R", "JR.DBT", "JJ.DBT"
                                    blnAdd = (CInt(m_objMySections.Section1.IRSRSH) = 1)
                            End Select

                        Case Else
                            Throw New ArgumentException
                    End Select

                Case 3 'Generation rate at each wavelength
                    blnAdd = strName.EndsWith("GN") AndAlso strExtension = "DAT"

                Case Else
                    Throw New ArgumentException
            End Select

            If blnAdd Then objFileList.Add(objOutputFile)
        Next

        Return objFileList
    End Function

    Public Function OptionList(ByVal listOption As OptionConstants, Optional ByVal typeIndex As Integer = 0, Optional ByVal itemIndex As Integer = 0) As List(Of String)
        Dim objOptionList As New List(Of String)

        Select Case listOption
            Case OptionConstants.Type
                For intIndex As Integer = 1 To 4
                    objOptionList.Add(Language.ReadMessages("msgOUTFILETYPE_" + intIndex.ToString))
                Next

            Case OptionConstants.Group
                Select Case typeIndex
                    Case 1 'Non-Equilibrium
                        For intIndex As Integer = 1 To 2
                            objOptionList.Add(Language.ReadMessages("msgNONEQ_" + intIndex.ToString))
                        Next
                        For intIndex As Integer = 1 To 3
                            objOptionList.Add(Language.ReadMessages("msgDPEXT_" + intIndex.ToString))
                        Next

                    Case 2 'Characteristic Output Device Curves
                        For intIndex As Integer = 1 To 2
                            objOptionList.Add(Language.ReadMessages("msgCURVES_" + intIndex.ToString))
                        Next
                End Select

            Case OptionConstants.Voltage
                Select Case typeIndex
                    Case 1 'Non-Equilibrium
                        Dim objSection2 As Section2 = m_objMySections.Section2

                        Select Case itemIndex
                            Case 0 'No Bias Light present
                                For Each objValue As Double In objSection2.VDARK
                                    objOptionList.Add(objValue.ToString)
                                Next

                            Case 1 'Bias Light present
                                For Each objValue As Double In objSection2.VLIGHT
                                    objOptionList.Add(objValue.ToString)
                                Next
                        End Select
                End Select

            Case OptionConstants.Special
                Select Case typeIndex
                    Case 1 'Non-Equilibrium
                        objOptionList.Add(Language.ReadMessages("msgNONE"))
                        For intIndex As Integer = 1 To 14
                            objOptionList.Add(Language.ReadMessages("msgSPECIAL_" + intIndex.ToString))
                        Next
                End Select

            Case OptionConstants.Region
                Select Case typeIndex
                    Case 1 'Non-Equilibrium
                        If itemIndex > 2 Then 'Only Gap Energy
                            objOptionList.AddRange(m_objMySections.Regions.ItemList)
                        End If
                End Select
        End Select

        Return objOptionList
    End Function

    Public Sub Publish(ByVal publishDirPath As String)
        m_strPublishDirPath = publishDirPath
        Me.Build()
        m_strPublishDirPath = Nothing
    End Sub

    Public Sub ResetData(Optional ByVal outputDirPath As String = Nothing)
        m_strOutputDirPath = outputDirPath
        m_objOutputFileList.Clear()
        m_blnUpdated = False
    End Sub

    Public ReadOnly Property Updated() As Boolean
        Get
            Return m_blnUpdated
        End Get
    End Property


    Private Sub m_objMySections_Changed() Handles m_objMySections.Changed
        If m_objMySections.Modified Then Me.ResetData()
    End Sub


    Private Sub BeginOutput()
        m_blnCanceled = False
        m_intErrorCode = 0

        With My.Computer.FileSystem
            If m_intOutputDirIndex = -1 Then
                Dim blnCreated As Boolean

                'Create output dir
                For index As Integer = 0 To 99
                    My.Application.DoEvents()
                    m_intOutputDirIndex = index
                    If Not .DirectoryExists(Me.OutputDirPath) Then
                        .CreateDirectory(Me.OutputDirPath)
                        blnCreated = True
                        Exit For
                    End If
                Next
                If Not blnCreated Then
                    m_intOutputDirIndex = -1
                    Throw New IO.DirectoryNotFoundException
                End If
            Else
                'Clean up output dir
                For Each strFile As String In .GetFiles(Me.OutputDirPath)
                    .DeleteFile(strFile)
                    My.Application.DoEvents()
                Next
            End If

            'Copy parameter file
            .CopyFile(.CombinePath(WorkingDirPath, FAC_FILE), .CombinePath(Me.TempDirPath, FAC_FILE))
            My.Application.DoEvents()

            'Copy case file
            .WriteAllText(.CombinePath(Me.TempDirPath, CASE_FILE), m_objMySections.Read, False)
            My.Application.DoEvents()
        End With
    End Sub

    Private Sub DoOutput()
        Dim blnDefectPoolRequired As Boolean
        Dim blnForwardVoltages As Boolean
        Dim blnReverseVoltages As Boolean
        Dim blnStabilizedStateRequired As Boolean
        Dim bytDPEF As Byte
        Dim intRegionCount As Integer = m_objMySections.Regions.Count
        Dim objSection1 As Section1 = m_objMySections.Section1
        Dim objSection5 As Section5

        'Defect Pool
        For intRegion As Integer = 1 To intRegionCount
            objSection5 = m_objMySections.Section5(intRegion)
            If CInt(objSection5.IDEP) <> 0 Then
                blnDefectPoolRequired = True
                Exit For
            End If
        Next

        'Stabilized State
        For intRegion As Integer = 1 To intRegionCount
            objSection5 = m_objMySections.Section5(intRegion)
            Select Case CInt(objSection5.IDEP)
                Case 4, 6
                    blnDefectPoolRequired = True
                    Exit For
            End Select
        Next

        With objSection1
            'Forward Voltages
            blnForwardVoltages = (.VOLT2 > 0)

            'Reverse Voltages
            blnReverseVoltages = (.VOLT1 < 0)
        End With

        If blnDefectPoolRequired Then
            Me.ExecutePREQ(1)
            Me.ExecutePREQ(2)
            bytDPEF = 2
        Else
            Me.ExecutePREQ(0)
            bytDPEF = 0
        End If

        If blnStabilizedStateRequired Then
            If blnForwardVoltages Then
                Me.ExecutePRNES(True, 1, bytDPEF)
                Me.ExecutePRNES(True, 2, bytDPEF)

                Me.ExecutePRNET(True, 1, bytDPEF)
                Me.ExecutePRNET(True, 2, bytDPEF)
            End If
            If blnReverseVoltages Then
                Me.ExecutePRNES(False, 1, bytDPEF)
                Me.ExecutePRNES(False, 2, bytDPEF)

                Me.ExecutePRNET(False, 1, bytDPEF)
                Me.ExecutePRNET(False, 2, bytDPEF)
            End If
        Else
            If blnForwardVoltages Then
                Me.ExecutePRNES(True, 0, bytDPEF)

                Me.ExecutePRNET(True, 0, bytDPEF)
            End If
            If blnReverseVoltages Then
                Me.ExecutePRNES(False, 0, bytDPEF)

                Me.ExecutePRNET(False, 0, bytDPEF)
            End If
        End If
    End Sub

    Private Sub EndOutput()
        With My.Computer.FileSystem
            'Rename odd files
            For Each file As String In .GetFiles(Me.OutputDirPath)
                If m_blnCanceled Then Exit For
                If .GetName(file).StartsWith("_") Then
                    .RenameFile(file, .GetName(file).Substring(1))
                End If
                My.Application.DoEvents()
            Next

            'Copy files to main temp dir
            For Each file As String In .GetFiles(Me.TempDirPath)
                If m_blnCanceled Then Exit For
                .MoveFile(file, .CombinePath(Me.OutputDirPath, .GetName(file)))
                My.Application.DoEvents()
            Next

            'Delete temp dir
            Try
                .DeleteDirectory(Me.TempDirPath, FileIO.DeleteDirectoryOption.DeleteAllContents)
            Catch ex As Exception
            End Try
            My.Application.DoEvents()
        End With
    End Sub

    Private Sub ExecutePREQ(ByVal DPEF As Byte)
        If Not m_blnCanceled AndAlso m_intErrorCode = 0 Then
            Dim strFile As String = My.Computer.FileSystem.CombinePath(Me.WorkingDirPath, PREQ_FILE)
            Dim objProcess As New System.Diagnostics.Process

            objProcess.StartInfo.FileName = strFile
            objProcess.StartInfo.WorkingDirectory = Me.WorkingDirPath()
            objProcess.StartInfo.Arguments = Me.TempDirPath + " " + DPEF.ToString
            objProcess.StartInfo.CreateNoWindow = True
            objProcess.StartInfo.RedirectStandardOutput = True
            objProcess.StartInfo.UseShellExecute = False

            objProcess.Start()
            Do
                With objProcess.StandardOutput
                    If .EndOfStream Then
                        RaiseEvent Status(.ReadToEnd)
                    Else
                        RaiseEvent Status(.ReadLine)
                    End If
                End With
                My.Application.DoEvents()
            Loop Until m_blnCanceled OrElse objProcess.HasExited

            If objProcess.HasExited Then
                m_intErrorCode = objProcess.ExitCode

                objProcess.Close()
            Else
                objProcess.Kill()
            End If

            objProcess.Dispose()
        End If
    End Sub

    Private Sub ExecutePRNES(ByVal forwardVoltage As Boolean, ByVal DPST As SByte, ByVal DPEF As Byte)
        If Not m_blnCanceled AndAlso m_intErrorCode = 0 Then
            Dim strFile As String = My.Computer.FileSystem.CombinePath(Me.WorkingDirPath, PRNES_FILE)
            Dim objProcess As New System.Diagnostics.Process
            Dim VOLRA As String = IIf(forwardVoltage, "FOR", "REV").ToString

            objProcess.StartInfo.FileName = strFile
            objProcess.StartInfo.WorkingDirectory = Me.WorkingDirPath()
            objProcess.StartInfo.Arguments = Me.TempDirPath + " " + VOLRA + " " + DPST.ToString + " " + DPEF.ToString
            objProcess.StartInfo.CreateNoWindow = True
            objProcess.StartInfo.RedirectStandardOutput = True
            objProcess.StartInfo.UseShellExecute = False

            objProcess.Start()
            Do
                With objProcess.StandardOutput
                    If .EndOfStream Then
                        RaiseEvent Status(.ReadToEnd)
                    Else
                        RaiseEvent Status(.ReadLine)
                    End If
                End With
                My.Application.DoEvents()
            Loop Until m_blnCanceled OrElse objProcess.HasExited

            If objProcess.HasExited Then
                m_intErrorCode = objProcess.ExitCode

                objProcess.Close()

                If Not m_blnCanceled Then
                    With My.Computer.FileSystem
                        If forwardVoltage Then
                            strFile = .CombinePath(Me.TempDirPath, "JD.DBF")
                            If .FileExists(strFile) Then .RenameFile(strFile, "JD.F")

                            strFile = .CombinePath(Me.TempDirPath, "JR.DBF")
                            If .FileExists(strFile) Then .RenameFile(strFile, "JR.F")

                            For Each strFile In .GetFiles(Me.TempDirPath, FileIO.SearchOption.SearchTopLevelOnly, New String() {"*.DBR"})
                                .DeleteFile(strFile)
                            Next
                        Else
                            strFile = .CombinePath(Me.TempDirPath, "JD.DBR")
                            If .FileExists(strFile) Then .RenameFile(strFile, "JD.R")

                            strFile = .CombinePath(Me.TempDirPath, "JR.DBR")
                            If .FileExists(strFile) Then .RenameFile(strFile, "JR.R")

                            For Each strFile In .GetFiles(Me.TempDirPath, FileIO.SearchOption.SearchTopLevelOnly, New String() {"*.DBF"})
                                .DeleteFile(strFile)
                            Next
                        End If
                    End With

                    My.Application.DoEvents()
                End If
            Else
                objProcess.Kill()
            End If

            objProcess.Dispose()
        End If
    End Sub

    Private Sub ExecutePRNET(ByVal forwardVoltage As Boolean, ByVal DPST As SByte, ByVal DPEF As Byte)
        If Not m_blnCanceled AndAlso m_intErrorCode = 0 Then
            Dim strFile As String = My.Computer.FileSystem.CombinePath(Me.WorkingDirPath, PRNET_FILE)
            Dim objProcess As New System.Diagnostics.Process
            Dim VOLRA As String = IIf(forwardVoltage, "FOR", "REV").ToString

            objProcess.StartInfo.FileName = strFile
            objProcess.StartInfo.WorkingDirectory = Me.WorkingDirPath()
            objProcess.StartInfo.Arguments = Me.TempDirPath + " " + VOLRA + " " + DPST.ToString + " " + DPEF.ToString
            objProcess.StartInfo.CreateNoWindow = True
            objProcess.StartInfo.RedirectStandardOutput = True
            objProcess.StartInfo.UseShellExecute = False

            objProcess.Start()
            Do
                With objProcess.StandardOutput
                    If .EndOfStream Then
                        RaiseEvent Status(.ReadToEnd)
                    Else
                        RaiseEvent Status(.ReadLine)
                    End If
                End With
                My.Application.DoEvents()
            Loop Until m_blnCanceled OrElse objProcess.HasExited

            If objProcess.HasExited Then
                m_intErrorCode = objProcess.ExitCode

                objProcess.Close()

                If Not m_blnCanceled Then
                    With My.Computer.FileSystem
                        If forwardVoltage Then
                            For Each strFile In .GetFiles(Me.TempDirPath, FileIO.SearchOption.SearchTopLevelOnly, New String() {"*.DBR"})
                                .DeleteFile(strFile)
                            Next
                        Else
                            For Each strFile In .GetFiles(Me.TempDirPath, FileIO.SearchOption.SearchTopLevelOnly, New String() {"*.DBF"})
                                .DeleteFile(strFile)
                            Next
                        End If
                    End With

                    My.Application.DoEvents()
                End If
            Else
                objProcess.Kill()
            End If

            objProcess.Dispose()
        End If
    End Sub

    Private Sub ReadOutputDir()
        Dim strName As String
        Dim strBaseName As String

        m_objOutputFileList.Clear()
        For Each strFile As String In My.Computer.FileSystem.GetFiles(Me.OutputDirPath)
            If m_blnCanceled Then Exit For

            strName = My.Computer.FileSystem.GetName(strFile)
            strBaseName = strName.Substring(0, strName.IndexOf("."))

            If strName = CASE_FILE OrElse strName = FAC_FILE Then
                My.Computer.FileSystem.DeleteFile(strFile)
            Else
                Dim objOutputFile As New OutputFile
                Dim strExtension As String = strName.Remove(0, strBaseName.Length + 1)

                If IsNumeric(strBaseName.Substring(0, 2)) Then
                    If strExtension = "DAT" Then
                        objOutputFile.Title = String.Format(Language.ReadMessages("msgGNX"), "0." + strBaseName.Substring(0, 2))
                    Else
                        objOutputFile.Title = Language.ReadMessages("msg" + strBaseName.Substring(2))
                    End If
                ElseIf IsNumeric(strExtension) OrElse strExtension = "JSC" OrElse strExtension = "FFF" OrElse strExtension = "VOC" OrElse strExtension = "DAT" Then
                    objOutputFile.Title = Language.ReadMessages("msg" + strBaseName)
                Else
                    objOutputFile.Title = Language.ReadMessages("msg" + strName)
                End If
                objOutputFile.Name = strName
                objOutputFile.Extension = strExtension
                m_objOutputFileList.Add(objOutputFile)
            End If
            My.Application.DoEvents()
        Next

        If m_blnCanceled Then
            m_objOutputFileList.Clear()
        ElseIf m_strPublishDirPath IsNot Nothing Then
            With My.Computer.FileSystem
                Try
                    .CopyDirectory(Me.OutputDirPath, m_strPublishDirPath, FileIO.UIOption.AllDialogs)
                Catch ex As Exception
                    m_blnCanceled = True
                    For Each strFile As String In .GetFiles(m_strPublishDirPath)
                        .DeleteFile(strFile)
                    Next
                End Try
            End With
        End If
    End Sub

    Private Function OutputDirPath() As String
        If m_strOutputDirPath Is Nothing Then
            Dim strPath As String

            With My.Computer.FileSystem
                strPath = .CombinePath(.CombinePath(.GetDriveInfo(.SpecialDirectories.Temp).Name, TEMP_DIR), m_intOutputDirIndex.ToString("00"))
            End With

            Return strPath
        Else
            Return m_strOutputDirPath
        End If
    End Function

    Private Function TempDirPath() As String
        Dim strPath As String

        With My.Computer.FileSystem
            strPath = .CombinePath(Me.OutputDirPath, "__")
        End With

        Return strPath + System.IO.Path.DirectorySeparatorChar
    End Function

    Private Function WorkingDirPath() As String
        Return My.Computer.FileSystem.CombinePath(My.Application.Info.DirectoryPath, "Core")
    End Function

End Class
